package org.liuhy.jbpm4
{
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.line.rect.RectLine;
	import org.liuhy.node.FlowNode;
	
	
	public class JbpmGraphicFactory
	{
		//流程节点工厂;
		public function JbpmGraphicFactory()
		{
		}
		public static function getFlowNodeByXML(p_xml:XML):FlowNode{
			var a:int = p_xml.toXMLString().indexOf("<task");
			
			if(p_xml.toXMLString().indexOf("<task")==0){
				var _g:String = p_xml.attribute("g").toString();
				var _posArray:Array = _g.split(/,/);
				var  _flowNode:TaskFlowNode = new TaskFlowNode(int(_posArray[0]),int(_posArray[1]),int(_posArray[2]),int(_posArray[3]));
				//设置属性;
				_flowNode.title =  p_xml.@name.toString();
				_flowNode.form 	= p_xml.@form.toString();
				var _tasktype="none";
				if(p_xml.toXMLString().indexOf("assignee=")>-1){
					_tasktype="assignee";
				}else if(p_xml.toXMLString().indexOf("candidate-groups")>-1){
					_tasktype="candidate-groups";
				}else if(p_xml.toXMLString().indexOf("candidate-users")>-1){
					_tasktype="candidate-users";
				}else if(p_xml.toXMLString().indexOf("swimlane")>-1){
					_tasktype="swimlane";
				}else if(p_xml.toXMLString().indexOf("none")>-1){
					_tasktype="none";
				}
				_flowNode.taskType = _tasktype;
				_flowNode.expr =p_xml.attribute(_tasktype).toString();
				return _flowNode;	
			}else if(p_xml.toXMLString().indexOf("<decision")==0){
				var _g:String = p_xml.attribute("g").toString();
				var _posArray:Array = _g.split(/,/);
				var  _decisionFlowNode:DecisionFlowNode = new DecisionFlowNode(int(_posArray[0]),int(_posArray[1]),int(_posArray[2]),int(_posArray[3]));
				//设置属性;
				_decisionFlowNode.title 	=  p_xml.@name.toString();
				_decisionFlowNode.language 	=  p_xml.@lang.toString();
				_decisionFlowNode.expr 		=  p_xml.@expr.toString();
				return _decisionFlowNode;
			}else if(p_xml.toXMLString().indexOf("<start")==0){
				var _g:String = p_xml.attribute("g").toString();
				var _posArray:Array = _g.split(/,/);
				var  _startFlowNode:StartFlowNode = new StartFlowNode(int(_posArray[0]),int(_posArray[1]),int(_posArray[2]),int(_posArray[3]));
				//设置属性;
				_startFlowNode.title 	=  p_xml.@name.toString();
				return _startFlowNode;
			}else if(p_xml.toXMLString().indexOf("<end")==0){
				var _g:String = p_xml.attribute("g").toString();
				var _posArray:Array = _g.split(/,/);
				var  _endFlowNode:EndFlowNode = new EndFlowNode(int(_posArray[0]),int(_posArray[1]),int(_posArray[2]),int(_posArray[3]));
				//设置属性;
				_endFlowNode.title 	=  p_xml.@name.toString();
				return _endFlowNode;
			}
			
//			if("task"==p_type){
//				return  new TaskFlowNode(p_x,p_y,p_width,p_height);				
//			}else if("decision"==p_type){
//				return  new DecisionFlowNode(p_x,p_y,p_width,p_height);
//			}else if("hql"==p_type){
//				return  new HQLFlowNode(p_x,p_y,p_width,p_height);			
//			}else if("start"==p_type){
//				return new StartFlowNode(p_x,p_y,p_width,p_height);				
//			}else if("end"==p_type){
//				return new EndFlowNode(p_x,p_y,p_width,p_height);				
//			}
			return null;
		}
		
		public static function getFlowNode(p_type:String,p_x:int,p_y:int,p_width:int,p_height:int):FlowNode{
			if("task"==p_type){
				return  new TaskFlowNode(p_x,p_y,p_width,p_height);				
			}else if("decision"==p_type){
				return  new DecisionFlowNode(p_x,p_y,p_width,p_height);
			}else if("hql"==p_type){
				return  new HQLFlowNode(p_x,p_y,p_width,p_height);			
			}else if("start"==p_type){
				return new StartFlowNode(p_x,p_y,p_width,p_height);				
			}else if("end"==p_type){
				return new EndFlowNode(p_x,p_y,p_width,p_height);				
			}
			return null;
		}
		public static function createNEW(p_type:String,p_point:Point):UIComponent{
			if("line"==p_type){
				var _cLine:CLine = new CLine();
				var _line:AbstractLine = new RectLine(p_point.x,p_point.y,p_point.x+200,p_point.y,_cLine);
				_line.draw();
				return _cLine;
			}else if("task"==p_type){
				var _rect:FlowNode 	= new TaskFlowNode(p_point.x,p_point.y,100,75);
				_rect.draw();
				return _rect;
			}else if("decision"==p_type){
				var _rect:FlowNode 	= new DecisionFlowNode(p_point.x,p_point.y,100,75);
				_rect.draw();
				return _rect;
			}else if("hql"==p_type){
				var _rect:FlowNode 	= new HQLFlowNode(p_point.x,p_point.y,100,75);
				_rect.draw();
				return _rect;
			}else if("start"==p_type){
				var _rect:FlowNode 	= new StartFlowNode(p_point.x,p_point.y,50,50);
				_rect.draw();
				return _rect;
			}else if("end"==p_type){
				var _rect:FlowNode 	= new EndFlowNode(p_point.x,p_point.y,50,50);
				_rect.draw();
				return _rect;
			}
			return null;
		}
	}
}